<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <button onclick="dbfn()">debounce</button>
  <button onclick="thfn()">throttle</button>
  <script>
    //防抖
    let debounce = function(fn,timestamp){
      let timer = null
      return function(){
        clearTimeout(timer)
        timer = setTimeout(()=>{
          fn.call(this,arguments)
        },timestamp)
      }
    }
    let dbfn = debounce(function(){
      console.log('debounce');
    },300)


    //截流
    let throttle = function(fn,timestamp){
      let timer = null
      return function(){
        console.log(timer);
        if(timer){
          return
        }
        timer = setTimeout(()=>{
          fn.call(this,arguments)
          clearTimeout(timer)
          timer = null
        },timestamp)
      }
    }

    let thfn = throttle(function(){
      console.log('throttle');
    },1000)
  </script>
</body>
</html>